package com.hopedove.tempdemo.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.viewModelScope
import com.hadilq.liveevent.LiveEvent
import com.hopedove.net.kotlin.ErrorResult
import com.hopedove.net.kotlin.SuccessResult
import com.hopedove.tempdemo.net.SoapApi
import com.hopedove.tempdemo.repository.Repository
import com.hopedove.viewmodel.BaseViewModel
import kotlinx.coroutines.launch

class QueryNoticeListViewModel(private val api: SoapApi = SoapApi()) : BaseViewModel() {

    private val _navigate = LiveEvent<Boolean>()
    val navigate: LiveData<Boolean> = _navigate

    fun query(msg: String) = viewModelScope.launch {
        _loading.value = true
        api.queryNotice(msg).let { ret ->
            when (ret) {
                is SuccessResult -> {
                    Repository.saveNoticeList(ret.value)
                    _navigate.value = true
                }
                is ErrorResult -> {

                }
            }
        }
    }.invokeOnCompletion { _loading.value = false }

}